/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.anny.prodcuct.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.anny.prodcuct.entity.ProductSpecifications;
import com.jeeplus.modules.anny.prodcuct.mapper.ProductSpecificationsMapper;

/**
 * 商品规格描述表Service
 * @author zhengcq
 * @version 2018-10-17
 */
@Service
@Transactional(readOnly = true)
public class ProductSpecificationsService extends CrudService<ProductSpecificationsMapper, ProductSpecifications> {

	public ProductSpecifications get(String id) {
		return super.get(id);
	}
	
	public List<ProductSpecifications> findList(ProductSpecifications productSpecifications) {
		return super.findList(productSpecifications);
	}

	/**
	 * 根据商品ID获取商品所配置的规格
	 * 例如:{"颜色":["红"],"内存":["64G","32G"]}
	 * @param productId
	 * @return
	 */
	public JSONObject findListByProductId(String productId) {
		Map<String,List<String>> map = new HashMap<>();
		ProductSpecifications productSpecifications = new ProductSpecifications();
		productSpecifications.setProductId(productId);
		List<ProductSpecifications> list = super.findList(productSpecifications);
		if (list != null && list.size() >0){
			for (ProductSpecifications p:list){
				List<String> attrList = map.get(p.getProductAttr());
				if (attrList != null && attrList.size()>0){
					attrList.add(p.getDetailDescription());
				}else {
					attrList = new ArrayList<>();
					attrList.add(p.getDetailDescription());
				}
				map.put(p.getProductAttr(),attrList);
			}
		}
		return JSONObject.fromObject(map);
	}
	
	public Page<ProductSpecifications> findPage(Page<ProductSpecifications> page, ProductSpecifications productSpecifications) {
		return super.findPage(page, productSpecifications);
	}
	
	@Transactional(readOnly = false)
	public void save(ProductSpecifications productSpecifications) {
		super.save(productSpecifications);
	}
	
	@Transactional(readOnly = false)
	public void delete(ProductSpecifications productSpecifications) {
		super.delete(productSpecifications);
	}
	
	/**
	 * 根据商品删除规格
	 * @param id
	 */
	@Transactional(readOnly = false)
	public void deleteByLogicProductId(String id){
		mapper.deleteByLogicProductId(id);
	}
}